<!-- src/components/LoginComponent.vue -->
<template>
  <div class="login-content-box">
    <div style="background-color: white;  display: flex;">
      <!-- 左侧背景图 -->
      <div class="login-left">
      </div>
      <!-- 右侧表单区域 -->
      <div class="login-right">
        <div class="login-title" >账号注册</div>
        <div class="login-form">
          <el-form ref="RegisterFormRef" label-position="top" hide-required-asterisk="true" :rules="RegisterRules"  :model="RegisterForm"  class="login-form">

            <el-form-item label="用户名" class="block-label" prop="userName" label-width="85px">
              <el-input v-model="RegisterForm.userName"
                        placeholder="请输入用户名"
                        clearable
                        class="custom-height-input"
                        :prefix-icon="User"></el-input>
            </el-form-item>
            <el-form-item label="手机号" class="block-label" prop="phone" label-width="85px">
              <el-input v-model="RegisterForm.phone"
                        placeholder="请输入手机号"
                        clearable
                        class="custom-height-input"
                        :prefix-icon="Phone"></el-input>
            </el-form-item>
            <el-form-item label="密码" class="block-label" prop="password" label-width="85px">
              <el-input v-model="RegisterForm.password"
                        type="password"
                        clearable
                        placeholder="请输入密码"
                        class="custom-height-input"
                        :prefix-icon="Lock"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" class="block-label" prop="surePassword" label-width="85px" >
              <el-input v-model="RegisterForm.surePassword"
                        type="password"
                        clearable
                        placeholder="请再次输入密码"
                        class="custom-height-input"
                        :prefix-icon="Lock"></el-input>
            </el-form-item>

            <div style="margin-top: 10px;font-size: 14px; text-align: center;padding:15px 0; color: #8B919A">
             我已阅读并同意 <span  class="no-hover-underline">《服务条款》</span>和<span  class="no-hover-underline">《隐私政策》</span>
            </div>
            <el-form-item>
              <el-button type="primary" @click="handleRegister" style="width: 100%;">立即注册</el-button>
            </el-form-item>
            <div style="text-align: center;letter-spacing: 1px; font-size: 14px;margin-top: 15px">
              <span style="color: #5f6168;">已有账号?</span>
                <span style="color: #43A2A3;cursor: pointer;" @click="handleLogin">立即登录</span>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
// 引入图标组件
import { User, Lock,Phone } from '@element-plus/icons-vue';
// 背景图路径（你可以替换为实际路径）
import {reactive, ref} from "vue";
import { useRouter } from 'vue-router'
const router = useRouter();
import {ElMessage} from "element-plus";
import {register} from "@/api/login.js";
const emit = defineEmits(['switch-view']);

const RegisterForm = ref({
  userName: '',
  phone:'',
  password: '',
  surePassword:'',
  remember: true
});
// 表单引用
const RegisterFormRef = ref(null);
// 验证规则
const RegisterRules = {
  userName: [{ required: true, message: '请输入登录账号', trigger: 'blur' },],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
  ],
  surePassword: [{ required: true, message: '请再次输入密码', trigger: 'blur' }]
};

const  handleRegister = () => {
  RegisterFormRef.value.validate(async (valid) => {
    if (valid) {
      if (RegisterForm.password != RegisterForm.surePassword) {
        ElMessage.error('密码不一致!');
        return false;
      }
      const result = await register({userName: RegisterForm.value.userName, phone: RegisterForm.value.phone, password: RegisterForm.value.password});
      if (result.status == 200 && result.data.code == 200) {
        ElMessage.success('注册成功!');
        handleLogin();
      } else {
        ElMessage.error(result.message);
      }
    } else {
      // 验证失败
      return false;
    }
  });

};

// 去登录
const handleLogin = () => {
  emit('switch-view', 'LoginComponent');
};


</script>
<style scoped lang="scss">
.login-content-box{
  width: 100%;
  background-color: #f5f7fa;
  .login-left {
    width: 300px;
    margin-right: 10px;
    background-size: cover;        // 覆盖整个容器
    background: url("../assets/main_app/img.png") no-repeat center center;
    background-size: 100% 100%;
    borerder-radius: 10px !important;
  }
  .login-right {
    flex: 1;
    border-radius: 0 10px 10px 0;
    box-shadow: 0 2px 12px rgba(215, 85, 85, 0.1);
    padding: 20px 20px;
    .login-title{
      font-size: 22px;
      font-weight: bolder;
      padding-bottom: 20px;
      letter-spacing: 2px;
    }
    .login-form {
      .el-button{
        background-color: #43A2A3;
        letter-spacing: 2px;
        height: 40px;
        width: 100%;
      }

      .no-hover-underline{
        cursor: pointer;
        color: #43A2A3;
      }

      /* 自定义复选框选中颜色 */
      .custom-checkbox {
        /* 控制选中状态的背景色和边框色 */
        --el-checkbox-checked-bg-color: #43A2A3; /* 复选框选中后的背景色 */
        --el-checkbox-checked-border-color: #43A2A3; /* 复选框选中后的边框色 */
        --el-checkbox-indeterminate-bg-color: #43A2A3; /* 半选状态的背景色 */
        --el-checkbox-indeterminate-border-color: #43A2A3; /* 半选状态的边框色 */
      }
      /* 可选：修改复选框的大小 */
      .custom-checkbox .el-checkbox__inner {
        width: 16px;
        height: 16px;
      }
      /* 可选：修改勾选图标的颜色 */
      .custom-checkbox .el-checkbox__inner::after {
        border-color: #fff; /* 勾选图标的颜色 */
      }
    }
  }
}

/* 两端对齐的表单项样式 */
.checkbox-forgot-item {
  margin-top: 20px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}


.el-checkbox.is-checked .el-checkbox__label {
  color: #43A2A3;
}
.el-form-item{
  margin-bottom: 12px !important;
}
</style>
